﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using SubSonic.Repository;
using xtra.domain.Subsonic3;

namespace sb.web {
    [DataContract()]
    public class MenuStructureView {
        [DataMember]
        public string CodeName { get; set; }
        [DataMember]
        public string Id { get; set; }
        [DataMember]
        public string ParentId { get; set; }
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string URI { get; set; }
        [DataMember]
        public string URL { get; set; }
    }

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MenuDataService {
        IRepository repo = new SimpleRepository(SimpleRepositoryOptions.None);

        #region Core
        [OperationContract]
        public List<MenuStructureView> GetMenu(int menuId) {
            List<MenuStructureView> structure = new List<MenuStructureView>();
            (from a in repo.All<ContentVersion>()
             join b in repo.All<ContentEntity>() on a.EntityId equals b.Id
             join c in repo.All<MenuDatum>() on a.Id equals c.Id
             join d in repo.All<Menu>() on c.MenuId equals d.Id
             where c.MenuId == menuId
             select new
              {
                  d.CodeName,
                  b.ParentId,
                  b.Id,
                  c.Title,
                  c.URI,
                  c.URL
              }).ToList().ForEach(item =>
                 {
                     structure.Add(new MenuStructureView
                     {
                         CodeName = item.CodeName,
                         ParentId = item.ParentId.ToString(),
                         Id = item.Id.ToString(),
                         Title = item.Title,
                         URI = item.URI,
                         URL = item.URL
                     });
                 });

            return structure;
        }
        #endregion
    }
}
